<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class SRParserKML2GPX {
	var $_gpx="";
	
	function SRParserKML2GPX(){
		$this->_gpx='<?xml version="1.0"?>
		<gpx
		 version="1.0"
		 creator="Changoo - changoo.net"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xmlns="http://www.topografix.com/GPX/1/0"
		 xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">';
	}
	
	private function addTime(){
		$this->_gpx=$this->_gpx.'<time>'.date('Y-m-d').'T'.date('G:i:s').'</time>';
	}
	private function addWaypoint($lat,$long,$ele,$time,$name){
		$this->_gpx=$this->_gpx.'<wpt lat="'.$lat.'" lon="'.$long.'">
 											<ele>'.$ele.'</ele>
 											<time>'.$time.'</time>
 											<name>'.$name.'</name>
 											<desc><![CDATA['.$name.']]></desc>
										</wpt>';
	}
	private function addSimpleWaypoint($lat,$long){
		$this->_gpx=$this->_gpx.'<wpt lat="'.$lat.'" lon="'.$long.'">
										</wpt>';
	}
	// por cada waypoint creamos una etiqueta xml en el gpx de salida
	private function parseWP($mvalues,$i) 
	{	
		while($i<count($mvalues)){
			if($mvalues[$i]['tag']=='name'){
				$name=$mvalues[$i]["value"];
			}
			if($mvalues[$i]['tag']=='when'){
				$time=$mvalues[$i]["value"];
			}
			if($mvalues[$i]['tag']=='coordinates'){
				$coordenadas=explode(",",$mvalues[$i]["value"]);
				$long=$coordenadas[0];
				$lat=$coordenadas[1];
				$ele=$coordenadas[2];
			}
			$i++;
		}
		$this->addWaypoint($lat,$long,$ele,$time,$name);
	}
	private function parseLineString($mvalues,$i){
		while($i<count($mvalues)){
			if($mvalues[$i]['tag']=='coordinates'){
				$waypoints=explode(' ',$mvalues[$i]["value"]);
				for($k=0;$k<count($waypoints);$k++){
					$coordinates=explode(',',$waypoints[$k]);
					if(0<count($coordinates[0]) || 0<count($coordinates[1])){
						$this->addSimpleWaypoint($coordinates[1],$coordinates[0]);
					}
				}
			}
			$i++;
		}
	}
	function KML2GPX($data){
		$this->addTime();
		$xml_parser = xml_parser_create(); 
		xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($xml_parser, $data, $outArray);
	    xml_parser_free($xml_parser);
		
	    for ($i=0;$i<count($outArray);$i++){
	    	//cada vez que encuentra un placemark parsea a waypoint de gpx
	    	if($outArray[$i]['tag']=='Placemark'){
	    		$this->parseWP($outArray,$i);
	    	}
	    	//cada vez que encuentra un linestring parsea a waypoints
	    	if($outArray[$i]['tag']=='LineString'){
	    		$this->parseLineString($outArray,$i);
	    	}
	    }
	    $this->_gpx=$this->_gpx.'</gpx>';
		return $this->_gpx;
	}
}

class SRParserGPX2LineString {
	var $_kml=null;
	
	function SRParserGPX2LineString(){
		$this->_kml='<LineString><coordinates>';
	}
	
	function parseWaypoint($track_point){
		$lat=$track_point['lat'];
		$long=$track_point['lon'];
		
		$this->_kml=$this->_kml.$long.','.$lat.' ';
	}
	function GPX2LineString($file_url){
		//TODO: En plugins/system/plgjoomsocialconnect se ha comentado JString::_strireplace
		//porque causaba bucle infinito
	
		$xml_data = simplexml_load_file($file_url);
		
		foreach($xml_data->trk[0]->trkseg->trkpt as $track_point){
			$this->parseWaypoint($track_point);
		}
	  
	    $this->_kml=$this->_kml.'</coordinates></LineString>';
	    
	    print_r($this->_kml);
	  
		return $this->_kml;
	}
}

class SRGeocoder{
	var $_service_url=null;
	
	function SRGeocoder(){
		$this->_service_url = 'http://maps.google.com/maps/api/geocode/xml';
	}
	
	public function geocode($address){
		$address=preg_replace('/ /', '+', $address);
		
		$curl = curl_init($this->_service_url.'?address='.$address.'&sensor=false');
		curl_setopt($curl, CURLOPT_HEADER, false); 
       	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       	$curl_response = curl_exec($curl);
       	curl_close($curl);

       	$xml = new SimpleXMLElement($curl_response);
       	$xmlCoords=$xml->result[0]->geometry[0]->location[0];
       	return DTOFactory::getInstance()->getDTOCoordinatesFromXML($xmlCoords);
	}
}